#include <cstdio>
#include <vector>
#include <algorithm>


std::vector<int> boxes[110];

/**
 * 时间复杂度
 * 初始化：O(C) (C为巧克力总数)
 * 操作部分：O(mC)
 * 输出部分：O(n+C)
 * 总体：O(n+mC)
 * 
 * 空间复杂度：O(C)
 * @return
 */
int main() {

	int n, m;
	scanf("%d%d", &n, &m);

	for (int i = 1; i <= n; i++) {
		int cnt;
		scanf("%d", &cnt);

		for (int j = 0; j < cnt; j++) {
			int chocolate;
			scanf("%d", &chocolate);
			boxes[i].push_back(chocolate);
		}

	}


	while (m--) {
		int op, x, y;
		scanf("%d", &op);
		if (op == 1) {
			scanf("%d%d", &x, &y);
			for (int i = 0; i < boxes[x].size(); i++) {
				boxes[x][i]--;
				if (boxes[x][i] > 0) {
					boxes[y].push_back(boxes[x][i]);
				}
			}
			boxes[x].clear();
		}
		else if (op == 2) {
			scanf("%d%d", &x, &y);
			for (int i = boxes[x].size() - 1; i >= 0; i--) {
				boxes[x][i]--;
				if (boxes[x][i] > 0) {
					boxes[y].push_back(boxes[x][i]);
				}
			}
			boxes[x].clear();
		}
		else {
			scanf("%d", &x);
			std::vector<int> tmp;
			std::swap(boxes[x], tmp);

			for (int i = tmp.size() - 1; i >= 0; i--) {
				tmp[i]--;
				if (tmp[i] > 0) {
					boxes[x].push_back(tmp[i]);
				}
			}

		}
	}

	for (int i = 1; i <= n; i++) {
		printf("%d", boxes[i].size());
		for (int j = 0; j < boxes[i].size(); j++) {
			printf(" %d", boxes[i][j]);
		}
		printf("\n");
	}


	return 0;
}